//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tzbank.poc.util;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.io.File;
import java.net.URL;
import java.util.*;

public final class Debug {
    private static final String noModuleModule = "NoModule";
    private static final Object[] emptyParams = new Object[0];
    public static final int ALWAYS = 0;
    public static final int VERBOSE = 1;
    public static final int TIMING = 2;
    public static final int INFO = 3;
    public static final int IMPORTANT = 4;
    public static final int WARNING = 5;
    public static final int ERROR = 6;
    public static final int FATAL = 7;
    private static final String[] levelProps = new String[8];
    private static final Map<String, Integer> levelStringMap = new HashMap();
    private static final boolean[] levelOnCache = new boolean[8];
//    private static final Logger root = buildLogger();
    private static final Map<Integer, Marker> levelsByMarkersMap = new HashMap();
    private static final Map<String, Integer> markersByHighLevelMap = new HashMap();
    private static final Map<String, Integer> markersByLowLevelMap = new HashMap();

    public Debug() {
    }

//    private static Logger buildLogger() {
//        LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
//
//        try {
//            if (StringUtils.isEmpty(System.getProperty("bosent.home"))) {
//                StartBosentForJar.setBosentHome();
//            }
//
//            URL url = UtilURL.fromBosentHomePathAndUnder("logback.xml");
//            File externalConfigFile = new File(url.getFile());
//            String externalConfigFileLocation = url.getFile();
//            if (!externalConfigFile.exists()) {
//                System.out.println("[WARNING] : Logback External Config File Parameter does not reference a file that exists");
//            } else {
//                JoranConfigurator configurator = new JoranConfigurator();
//                configurator.setContext(lc);
//                lc.reset();
//
//                try {
//                    configurator.doConfigure(externalConfigFileLocation);
//                } catch (JoranException var6) {
//                    var6.printStackTrace();
//                }
//
//                StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
//            }
//        } catch (Exception var7) {
//            System.out.println("[WARNING] : Logback.xml External Config File Parameter does not reference a file that exists");
//        }
//
//        return LoggerFactory.getLogger(Debug.class);
//    }

    public static String getMarkerByLevel(int level) {
        return levelsByMarkersMap != null && levelsByMarkersMap.get(level) != null ? ((Marker)levelsByMarkersMap.get(level)).getName() : ((Marker)levelsByMarkersMap.get(3)).getName();
    }

    public static int getHighLevelByMarker(String marker) {
        Integer level = (Integer)markersByLowLevelMap.get(marker);
        return level == null ? 1 : level;
    }

    public static int getLowLevelByMarker(String marker) {
        Integer level = (Integer)markersByLowLevelMap.get(marker);
        return level == null ? 1 : level;
    }

    public static Logger getLogger(String module) {
        return ValidateUtil.isNotEmpty(module) ? LoggerFactory.getLogger(module) : null;
    }

    public static Integer getLevelFromString(String levelName) {
        return levelName == null ? null : (Integer)levelStringMap.get(levelName.toLowerCase());
    }

    public static void log(int level, Throwable t, String msg, String module) {
        log(level, t, msg, module, "com.bosent.base.util.Debug", emptyParams);
    }

    public static void log(int level, Throwable t, String msg, String module, Object... params) {
        log(level, t, msg, module, "com.bosent.base.util.Debug", params);
    }

    public static void log(int level, Throwable t, String msg, String module, String callingClass) {
        log(level, t, msg, module, callingClass);
    }

    public static Map<String, String> getParaMapByStr(String param) {
        Map<String, String> paramMap = new HashMap();
        if (!StringUtil.isEmptyStr(param)) {
            String[] paramMaps = param.split(",");
            String[] var3 = paramMaps;
            int var4 = paramMaps.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String str = var3[var5];
                String[] mapParams = str.split(":");
                paramMap.put(mapParams[0].trim(), mapParams[1].trim());
            }
        }

        return paramMap;
    }

//    public static void logBytesTemplate(String templateName, byte[] bytesMsg, String encoding, String params, String module) {
//        Map<String, String> paramsMap = getParaMapByStr(params);
//        logBytesTemplate(templateName, bytesMsg, encoding, paramsMap, module);
//    }

//    public static void logBytesTemplate(String templateName, byte[] bytesMsg, String encoding, Map<String, String> params, String module) {
//        LogLocal logLocal = LogLocal.getInstance();
//        if (logLocal != null && LogLocal.getInstance().getAsynManager() != null) {
//            LogLocal.getInstance().getAsynManager().addLogBytesTemplateTask(templateName, bytesMsg, encoding, params, module);
//        } else {
//            logBytesTemplateCommand(Thread.currentThread().getId(), templateName, bytesMsg, encoding, params, module);
//        }
//
//    }

//    protected static void logBytesTemplateCommand(Long threadNo, String templateName, byte[] bytesMsg, String encoding, Map<String, String> params, String module) {
//        MsgTemplate template = LogLocal.getInstance().getMsgTemplate(templateName);
//        if (template == null) {
//            dealNoTemplateFound(templateName, module);
//        } else {
//            String logMsg = template.formatBytesMsg(bytesMsg, encoding, params);
//            logWithoutDefaultTemplate(threadNo, template.getLevel(), (Throwable)null, logMsg, module, (String)null);
//        }
//
//    }

    public static void dealNoTemplateFound(String templateName, String module) {
        logError("can't find template:" + templateName, module);
    }

//    public static void logXmlTemplate(String templateName, String xmlmsg, String params, String module) {
//        Map<String, String> paramsMap = getParaMapByStr(params);
//        logXmlTemplate(templateName, xmlmsg, paramsMap, module);
//    }

//    public static void logXmlTemplate(String templateName, String xmlmsg, Map<String, String> params, String module) {
//        LogLocal logLocal = LogLocal.getInstance();
//        if (logLocal != null && LogLocal.getInstance().getAsynManager() != null) {
//            LogLocal.getInstance().getAsynManager().addXmlTemplateTask(templateName, xmlmsg, params, module);
//        } else {
//            logXmlTemplateCommand(Thread.currentThread().getId(), templateName, xmlmsg, params, module);
//        }
//
//    }

//    protected static void logXmlTemplateCommand(Long threadNo, String templateName, String xmlmsg, Map<String, String> params, String module) {
//        DesensitizeUtil logutil = LogLocal.getInstance().getDesensitizeUtil();
//        xmlmsg = logutil.desensitizeXmlString(xmlmsg);
//        params.put("xmlmsg", xmlmsg);
//        MsgTemplate template = LogLocal.getInstance().getMsgTemplate(templateName);
//        if (template == null) {
//            dealNoTemplateFound(templateName, module);
//        } else {
//            String logMsg = template.formatMsg("{}", params);
//            logWithoutDefaultTemplate(threadNo, template.getLevel(), (Throwable)null, logMsg, module, (String)null);
//        }
//
//    }

    private static JSONObject getJSONObjectByStr(String msg) {
        return StringUtil.isEmptyStr(msg) ? new JSONObject() : JSONObject.parseObject(msg);
    }

    private static JSONObject getJSONObjectByMap(Map<String, Object> msg) {
        if (msg != null) {
            return JSONObject.class.isInstance(msg) ? (JSONObject)msg : JSONObject.parseObject(JSONObject.toJSONString(msg));
        } else {
            return new JSONObject();
        }
    }

//    public static void logTemplate(String templateName, String theMsg, String params, String module) {
//        JSONObject JsonMsg = getJSONObjectByStr(theMsg);
//        Map<String, String> paramsMap = getParaMapByStr(params);
//        logTemplate(templateName, JsonMsg, paramsMap, module);
//    }

//    public static void logTemplate(String templateName, Map<String, Object> theMsg, String params, String module) {
//        JSONObject jsonMsg = getJSONObjectByMap(theMsg);
//        Map<String, String> paramsMap = getParaMapByStr(params);
//        logTemplate(templateName, jsonMsg, paramsMap, module);
//    }

//    public static void logTemplate(String templateName, JSONObject msg, Map<String, String> params, String module) {
//        logTemplate(templateName, msg, (Throwable)null, params, module);
//    }

//    public static void logTemplate(String templateName, JSONObject msg, Throwable t, Map<String, String> params, String module) {
//        LogLocal logLocal = LogLocal.getInstance();
//        if (logLocal != null && LogLocal.getInstance().getAsynManager() != null) {
//            LogLocal.getInstance().getAsynManager().addTemplateTask(templateName, msg, t, params, module);
//        } else {
//            logTemplateCommand(Thread.currentThread().getId(), templateName, msg, t, params, module);
//        }
//
//    }

//    protected static void logTemplateCommand(Long threadNo, String templateName, JSONObject msg, Throwable t, Map<String, String> params, String module) {
//        DesensitizeUtil logutil = LogLocal.getInstance().getDesensitizeUtil();
//        logutil.desensitize(msg);
//        MsgTemplate template = LogLocal.getInstance().getMsgTemplate(templateName);
//        if (template == null) {
//            dealNoTemplateFound(templateName, module);
//        } else {
//            String logMsg = template.formatJsonMsg(msg, params);
//            logWithoutDefaultTemplate(threadNo, template.getLevel(), t, logMsg, module, (String)null);
//        }
//
//    }
//
//    public static void log(int level, Throwable t, String msg, String module, String callingClass, Object... params) {
//        if (isOn(level)) {
//            if (LogLocal.getInstance().isUseDefaultTemplate()) {
//                logWithDefaultTemplate(Thread.currentThread().getId(), level, t, msg, module, callingClass, params);
//            } else {
//                logWithoutDefaultTemplate(Thread.currentThread().getId(), level, t, msg, module, callingClass, params);
//            }
//        }
//
//    }

//    private static void logWithDefaultTemplate(Long threadNo, int level, Throwable t, String msg, String module, String callingClass, Object... params) {
//        LogLocal logLocal = LogLocal.getInstance();
//        if (logLocal != null && LogLocal.getInstance().getAsynManager() != null) {
//            LogLocal.getInstance().getAsynManager().addLogDefaultTemplateTask(level, t, msg, module, callingClass, params);
//        } else {
//            logDefaultTemplateCommand(threadNo, level, t, msg, module, callingClass, params);
//        }
//
//    }

//    protected static void logDefaultTemplateCommand(Long threadNo, int level, Throwable t, String msg, String module, String callingClass, Object... params) {
//        if (t != null) {
//            MsgTemplate exceptionTemplate = LogLocal.getInstance().getDefaultExceptionMsgTemplate();
//            if (exceptionTemplate == null) {
//                logCommand(threadNo, level, t, msg, module, callingClass, params);
//            } else {
//                if (msg == null) {
//                    msg = t.getMessage();
//                } else {
//                    msg = msg + t.getMessage();
//                }
//
//                Map<String, String> theParamMap = new HashMap();
//                theParamMap.put("exceptionMsg", t.getMessage());
//                msg = exceptionTemplate.formatMsg(msg, theParamMap);
//                logCommand(threadNo, level, t, msg, module, callingClass, params);
//            }
//        } else {
//            Object[] theTemplateAndMsg = LogLocal.getInstance().getDefaultMsgTemplateAndMsg(msg);
//            if (theTemplateAndMsg == null) {
//                logCommand(threadNo, level, t, msg, module, callingClass, params);
//            } else {
//                String logMsg = null;
//                MsgTemplate theTemplate = (MsgTemplate)theTemplateAndMsg[0];
//                if (JSONObject.class.isInstance(theTemplateAndMsg[1])) {
//                    JSONObject jsonMsg = (JSONObject)theTemplateAndMsg[1];
//                    logMsg = theTemplate.formatJsonMsg(jsonMsg, (Map)null);
//                } else {
//                    Map<String, String> theParaMap = new HashMap();
//                    theParaMap.put("plainStrOut", msg);
//                    logMsg = theTemplate.formatMsg(msg, theParaMap);
//                }
//
//                logCommand(threadNo, level, t, logMsg, module, callingClass, params);
//            }
//        }
//    }

//    private static void logWithoutDefaultTemplate(Long threadNo, int level, Throwable t, String msg, String module, String callingClass, Object... params) {
//        StringBuilder sb = new StringBuilder();
//        sb.append(" ").append(threadNo).append(" <" + Thread.currentThread().getName() + ">").append(" -  ");
//        if (t != null) {
//            if (msg == null) {
//                msg = sb.toString();
//            } else {
//                sb.append(msg);
//                msg = sb.toString();
//            }
//        } else {
//            sb.append(msg);
//            msg = sb.toString();
//        }
//
//        if (isOn(level)) {
//            LogLocal logLocal = LogLocal.getInstance();
//            if (logLocal != null && LogLocal.getInstance().getAsynManager() != null) {
//                LogLocal.getInstance().getAsynManager().addLogBasicTask(level, t, msg, module, callingClass, params);
//            } else {
//                logCommand(threadNo, level, t, msg, module, callingClass, params);
//            }
//        }
//
//    }

    protected static void logCommand(Long threadNo, int level, Throwable t, String msg, String module, String callingClass, Object... params) {
        if (msg != null && params.length > 0) {
            StringBuilder sb = new StringBuilder();
            Formatter formatter = new Formatter(sb);
            formatter.format(msg, params);
            msg = sb.toString();
            formatter.close();
        }

        Logger logger = getLogger(module);
        switch(level) {
            case 0:
                logger.debug((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 1:
                logger.debug((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 2:
                logger.info((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 3:
                logger.info((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 4:
                logger.warn((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 5:
                logger.warn((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 6:
                logger.error((Marker)levelsByMarkersMap.get(level), msg, t);
                break;
            case 7:
                logger.error((Marker)levelsByMarkersMap.get(level), msg, t);
        }

    }

    public static boolean isOn(int level) {
        return levelOnCache[level];
    }

    public static void log(String msg) {
        log(0, (Throwable)null, msg, "NoModule", (Object[])emptyParams);
    }

    public static void log(String msg, Object... params) {
        log(0, (Throwable)null, msg, "NoModule", (Object[])params);
    }

    public static void log(Throwable t) {
        log(0, t, (String)null, "NoModule", (Object[])emptyParams);
    }

    public static void log(String msg, String module) {
        log(0, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void log(String msg, String module, Object... params) {
        log(0, (Throwable)null, msg, module, (Object[])params);
    }

    public static void log(Throwable t, String module) {
        log(0, t, (String)null, module, (Object[])emptyParams);
    }

    public static void log(Throwable t, String msg, String module) {
        log(0, t, msg, module, (Object[])emptyParams);
    }

    public static void log(Throwable t, String msg, String module, Object... params) {
        log(0, t, msg, module, (Object[])params);
    }

    public static boolean verboseOn() {
        return isOn(1);
    }

    public static void logVerbose(String msg, String module) {
        log(1, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logVerbose(String msg, String module, Object... params) {
        log(1, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logVerbose(Throwable t, String module) {
        log(1, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logVerbose(Throwable t, String msg, String module) {
        log(1, t, msg, module, (Object[])emptyParams);
    }

    public static void logVerbose(Throwable t, String msg, String module, Object... params) {
        log(1, t, msg, module, (Object[])params);
    }

    public static boolean timingOn() {
        return isOn(2);
    }

    public static void logTiming(String msg, String module) {
        log(2, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logTiming(String msg, String module, Object... params) {
        log(2, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logTiming(Throwable t, String module) {
        log(2, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logTiming(Throwable t, String msg, String module) {
        log(2, t, msg, module, (Object[])emptyParams);
    }

    public static void logTiming(Throwable t, String msg, String module, Object... params) {
        log(2, t, msg, module, (Object[])params);
    }

    public static boolean infoOn() {
        return isOn(3);
    }

    public static void logInfo(String msg, String module) {
        log(3, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logInfo(String msg, String module, Object... params) {
        log(3, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logInfo(Throwable t, String module) {
        log(3, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logInfo(Throwable t, String msg, String module) {
        log(3, t, msg, module, (Object[])emptyParams);
    }

    public static void logInfo(Throwable t, String msg, String module, Object... params) {
        log(3, t, msg, module, (Object[])params);
    }

    public static boolean importantOn() {
        return isOn(4);
    }

    public static void logImportant(String msg, String module) {
        log(4, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logImportant(String msg, String module, Object... params) {
        log(4, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logImportant(Throwable t, String module) {
        log(4, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logImportant(Throwable t, String msg, String module) {
        log(4, t, msg, module, (Object[])emptyParams);
    }

    public static void logImportant(Throwable t, String msg, String module, Object... params) {
        log(4, t, msg, module, (Object[])params);
    }

    public static boolean warningOn() {
        return isOn(5);
    }

    public static void logWarning(String msg, String module) {
        log(5, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logWarning(String msg, String module, Object... params) {
        log(5, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logWarning(Throwable t, String module) {
        log(5, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logWarning(Throwable t, String msg, String module) {
        log(5, t, msg, module, (Object[])emptyParams);
    }

    public static void logWarning(Throwable t, String msg, String module, Object... params) {
        log(5, t, msg, module, (Object[])params);
    }

    public static boolean errorOn() {
        return isOn(6);
    }

    public static void logError(String msg, String module) {
        log(6, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logError(String msg, String module, Object... params) {
        log(6, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logError(Throwable t, String module) {
        log(6, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logError(Throwable t, String msg, String module) {
        log(6, t, msg, module, (Object[])emptyParams);
    }

    public static void logError(Throwable t, String msg, String module, Object... params) {
        log(6, t, msg, module, (Object[])params);
    }

    public static boolean fatalOn() {
        return isOn(7);
    }

    public static void logFatal(String msg, String module) {
        log(7, (Throwable)null, msg, module, (Object[])emptyParams);
    }

    public static void logFatal(String msg, String module, Object... params) {
        log(7, (Throwable)null, msg, module, (Object[])params);
    }

    public static void logFatal(Throwable t, String module) {
        log(7, t, (String)null, module, (Object[])emptyParams);
    }

    public static void logFatal(Throwable t, String msg, String module) {
        log(7, t, msg, module, (Object[])emptyParams);
    }

    public static void logFatal(Throwable t, String msg, String module, Object... params) {
        log(7, t, msg, module, (Object[])params);
    }

    public static void set(int level, boolean on) {
        levelOnCache[level] = on;
    }

    public static boolean get(int level) {
        return levelOnCache[level];
    }

    public static String getStr(Object... params) {
        if (params != null && params.length != 0) {
            StringBuilder sb = new StringBuilder();
            Object[] var2 = params;
            int var3 = params.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                Object param = var2[var4];
                appendValue(sb, param);
            }

            return sb.toString();
        } else {
            return null;
        }
    }

    private static void appendValue(StringBuilder sb, Object value) {
        if (value != null && value instanceof Map) {
            Map theParam = (Map)value;
            if (theParam.size() > 0) {
                Iterator var3 = theParam.keySet().iterator();

                while(var3.hasNext()) {
                    Object key = var3.next();
                    if (!"userLogin".equals(key)) {
                        sb.append(" ").append(key).append("=(");
                        appendValue(sb, theParam.get(key));
                        sb.append(") ");
                    }
                }
            }
        } else {
            sb.append(" ").append(value);
        }

    }

//    static {
//        levelStringMap.put("verbose", 1);
//        levelStringMap.put("timing", 2);
//        levelStringMap.put("info", 3);
//        levelStringMap.put("important", 4);
//        levelStringMap.put("warning", 5);
//        levelStringMap.put("error", 6);
//        levelStringMap.put("fatal", 7);
//        levelStringMap.put("always", 0);
//        String levelPropsPrefix = "print.";
//        levelProps[0] = "";
//
//        String levelString;
//        for(Iterator var1 = levelStringMap.keySet().iterator(); var1.hasNext(); levelProps[(Integer)levelStringMap.get(levelString)] = levelPropsPrefix + levelString) {
//            levelString = (String)var1.next();
//        }
//
//        Properties properties = UtilProperties.getProperties("debug.properties");
//        if (properties == null) {
//            throw new IllegalStateException("debug.properties file not found");
//        } else {
//            for(int i = 0; i < levelOnCache.length; ++i) {
//                levelOnCache[i] = i == 0 || "true".equalsIgnoreCase(properties.getProperty(levelProps[i]));
//            }
//
//            if (!StringUtil.isEmptyStr(properties.getProperty("markers"))) {
//                String[] markers = properties.getProperty("markers").split(",");
//
//                for(int i = 0; i < markers.length; ++i) {
//                    String markerMappingLevelsStr = properties.getProperty("marker." + markers[i]);
//                    if (!StringUtil.isEmptyStr(markerMappingLevelsStr)) {
//                        int highLevel = 1;
//                        int lowLevel = 7;
//                        String[] var7 = markerMappingLevelsStr.split(",");
//                        int var8 = var7.length;
//
//                        for(int var9 = 0; var9 < var8; ++var9) {
//                            String markerLevel = var7[var9];
//                            if (!StringUtil.isEmptyStr(markerLevel)) {
//                                Integer theLevel = (Integer)levelStringMap.get(markerLevel);
//                                levelsByMarkersMap.put(theLevel, MarkerFactory.getMarker(markers[i]));
//                                if (theLevel > highLevel) {
//                                    highLevel = theLevel;
//                                    markersByHighLevelMap.put(markers[i], theLevel);
//                                }
//
//                                if (theLevel < lowLevel) {
//                                    lowLevel = theLevel;
//                                    markersByLowLevelMap.put(markers[i], theLevel);
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//        }
//    }
}
